package cn.always.xiajia.admin.sys.controller.pc;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.always.xiajia.admin.client.api.sys.dto.AuthLoginReqVO;
import cn.always.xiajia.admin.sys.service.SysLoginService;
import cn.always.xiajia.admin.sys.service.SysMenuService;
import cn.always.xiajia.admin.sys.service.SysRoleMenuService;
import cn.always.xiajia.admin.sys.service.SysRoleService;
import cn.always.xiajia.admin.sys.service.SysUserRoleService;
import cn.always.xiajia.admin.sys.service.SysUserService;
import cn.always.xiajia.framework.common.config.SecurityProperties;
import cn.always.xiajia.framework.common.entity.AjaxResult;
import cn.always.xiajia.framework.common.util.ExtStrUtil;
import cn.always.xiajia.framework.mybatis.core.controller.BaseControllerX;
import cn.always.xiajia.framework.security.core.util.SecurityFrameworkUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "管理后台-登录中心")
@RestController
@RequestMapping("/admin/sys/syslogin")
@Slf4j
@Validated
public class SysLoginController extends BaseControllerX {

	@Resource
	protected SysLoginService sysLoginService;

	@Resource
	protected SecurityProperties securityProperties;

	@Resource
	protected SysUserService sysUserService;

	@Resource
	protected SysUserRoleService sysUserRoleService;

	@Resource
	protected SysRoleService sysRoleService;

	@Resource
	protected SysRoleMenuService sysRoleMenuService;

	@Resource
	protected SysMenuService sysMenuService;

	@PostMapping("/login")
	@PermitAll
	@Operation(summary = "使用账号密码登录")
	public AjaxResult login(@RequestBody @Valid AuthLoginReqVO reqVO) {
		return AjaxResult.success(sysLoginService.login(reqVO));
	}

	@PostMapping("/logout")
	@PermitAll
	@Operation(summary = "登出系统")
	public AjaxResult logout(HttpServletRequest request) {
		String token = SecurityFrameworkUtils.obtainAuthorization(request, securityProperties.getTokenHeader(),
				securityProperties.getTokenParameter());
		if (ExtStrUtil.isNotBlank(token)) {
			sysLoginService.logout(token, null);
		}
		return AjaxResult.success();
	}

	@PostMapping("/refreshtoken")
	@PermitAll
	@Operation(summary = "刷新令牌")
	@Parameter(name = "refreshToken", description = "刷新令牌", required = true)
	public AjaxResult refreshToken(@RequestParam("refreshToken") String refreshToken) {
		return AjaxResult.success(sysLoginService.refreshToken(refreshToken));
	}

	@GetMapping("/getpermissioninfo")
	@Operation(summary = "获取登录用户的权限信息")
	public AjaxResult getPermissionInfo() {
		return sysLoginService.getPermissionInfo();
	}

}
